﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using UtilZ.Dotnet.Ex.Base;

namespace UtilZ.Dotnet.Ex.Config
{
    /// <summary>
    /// 配置读写类
    /// </summary>
    internal partial class ConfigCore
    {
        private const string _DIC_ITEM_ELE_NAME = "KeyValuePair";
        private const string _KEY = "Key";
        private const string _VALUE = "Value";




        /// <summary>
        /// 构造函数
        /// </summary>
        public ConfigCore()
        {

        }




        #region Common
        private Dictionary<Type, PropertyInfo[]> _typePropertyInfoArrDic = null;
        private PropertyInfo[] GetTypePropertyInfos(Type type)
        {
            if (this._typePropertyInfoArrDic == null)
            {
                this._typePropertyInfoArrDic = new Dictionary<Type, PropertyInfo[]>();
            }

            PropertyInfo[] propertyInfoArr;
            if (!this._typePropertyInfoArrDic.TryGetValue(type, out propertyInfoArr))
            {
                propertyInfoArr = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                this._typePropertyInfoArrDic.Add(type, propertyInfoArr);
            }

            return propertyInfoArr;
        }


        private Dictionary<Type, TypeCategorys> _typeConfigDataTypeDic = null;
        private TypeCategorys GetDataType(Type type)
        {
            if (this._typeConfigDataTypeDic == null)
            {
                this._typeConfigDataTypeDic = new Dictionary<Type, TypeCategorys>();
            }

            TypeCategorys configDataType;
            if (!this._typeConfigDataTypeDic.TryGetValue(type, out configDataType))
            {
                configDataType = TypeEx.GetTypeTypeCategory(type);
                this._typeConfigDataTypeDic.Add(type, configDataType);
            }

            return configDataType;
        }


        private Dictionary<Type, Type> _typeElementTypeDic = null;
        private Type GetIListElementType(Type type)
        {
            if (this._typeElementTypeDic == null)
            {
                this._typeElementTypeDic = new Dictionary<Type, Type>();
            }

            Type eleType;
            if (this._typeElementTypeDic.TryGetValue(type, out eleType))
            {
                return eleType;
            }

            eleType = TypeEx.GetIListElementType(type);
            this._typeElementTypeDic.Add(type, eleType);
            return eleType;
        }

        private string GetIDictionaryName(string name, PropertyInfo propertyInfo)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                name = propertyInfo.Name;
            }
            return name;
        }

        private string GetIDictionaryElementName(string eleName)
        {
            if (string.IsNullOrWhiteSpace(eleName))
            {
                eleName = _DIC_ITEM_ELE_NAME;
            }

            return eleName;
        }

        private string GetConfigItemName(PropertyInfo propertyInfo)
        {
            return propertyInfo.Name;
        }

        private Dictionary<Type, string> _typeIListElementNameDic = null;
        private string GetIListElementName(Type eleType)
        {
            if (this._typeIListElementNameDic == null)
            {
                this._typeIListElementNameDic = new Dictionary<Type, string>();
            }

            string iListElementName;
            if (!this._typeIListElementNameDic.TryGetValue(eleType, out iListElementName))
            {
                iListElementName = TypeEx.GetGenericTypeName(eleType);
                this._typeIListElementNameDic.Add(eleType, iListElementName);
            }

            return iListElementName;
        }
        #endregion

    }
}
